package com.cheng.io.output;

import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 数据字节输出流
 * 构造器：DataOutputStream(OutputStream out) 需传入节点流，DataOutputStream为包装流
 * 通过DataOutputStream写入的数据只能使用DataInputStream读取
 */
public class DataOutputStreamTest01 {
    public static void main(String[] args) {
        try (
                DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream("io/resources/data"));
        ) {
            byte b = 127;
            short s = 3123;
            int i = 1312333;
            long l = 2312333L;
            float f = 33.1F;
            double d = 3.14D;
            boolean flag = true;
            char c = '若';
            String str = "hello world";

            dataOutputStream.writeByte(b);
            dataOutputStream.writeShort(s);
            dataOutputStream.writeInt(i);
            dataOutputStream.writeLong(l);
            dataOutputStream.writeFloat(f);
            dataOutputStream.writeDouble(d);
            dataOutputStream.writeBoolean(flag);
            dataOutputStream.writeChar(c);
            dataOutputStream.writeUTF(str);

            dataOutputStream.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
